﻿package com.bizmlib.utils
{
	import flash.geom.Point;
	
	
	public function getLinesIntersection(x11:Number, y11:Number, x12:Number, y12:Number, x21:Number, y21:Number, x22:Number, y22:Number):Point
	{
		var slope1:Number = getLineSlope(x11, y11, x12, y12);
		var slope2:Number = getLineSlope(x21, y21, x22, y22);
		
		if(slope1==slope2)
		{
			return null;
		}
		
		var xi:Number;
		var yi:Number;
		
		//if(slope1==Number.POSITIVE_INFINITY)
		if(!isFinite(slope1))
		{
			xi = x11;
			yi = x11*slope2 + getLineYIntercept(x21, y21, slope2);
		}
		//else if(slope1==Number.POSITIVE_INFINITY)
		else if(!isFinite(slope2))
		{
			xi = x21;
			yi = x21*slope1 + getLineYIntercept(x11, y11, slope1);
		}
		else
		{
			var yIntersect1:Number = getLineYIntercept(x11, y11, slope1);
			var yIntersect2:Number = getLineYIntercept(x21, y21, slope2);
			
			xi = (yIntersect1 - yIntersect2) / (slope2 - slope1);
			yi = slope1*xi + yIntersect1;
		}
		
		return new Point(xi, yi);
	}
	/**
	public function getLinesIntersection(slope1:Number, yIntersect1:Number, slope2:Number, yIntersect2:Number):Point
	{
		if(slope1==slope2)
		{
			return null;
		}
		
		var x:Number = (yIntersect1 - yIntersect2) / (slope2 - slope1);
		var y:Number = slope1*x + yIntersect1;
		
		return new Point(x, y);
	}
	/**/
}